package fight

import (
	"sync"
)

type GameManager struct {
	games map[uint64]*Game
	mu    sync.RWMutex
}

var GameMgr *GameManager = &GameManager{
	games: make(map[uint64]*Game),
}

func (gm *GameManager) Put(id uint64, g *Game) {
	gm.mu.Lock()
	defer gm.mu.Unlock()

	gm.games[id] = g
}

func (gm *GameManager) Get(id uint64) (*Game, bool) {
	gm.mu.RLock()
	defer gm.mu.RUnlock()

	g, ok := gm.games[id]
	return g, ok
}

func (gm *GameManager) Del(id uint64) {
	gm.mu.Lock()
	defer gm.mu.Unlock()

	delete(gm.games, id)
}

func (gm *GameManager) AllGameIds() []uint64 {
	gm.mu.Lock()
	defer gm.mu.Unlock()
	ids := make([]uint64, 0, len(gm.games))
	for id, _ := range gm.games {
		ids = append(ids, id)
	}
	return ids
}
